import Vue from 'vue'
export default {
    namespaced: true,
    state: () => ({
        // 存放商品信息
        goodsList:[],
        addressInfo: [
            {
                id:1,
                name:'张三',
                tel:'12345678901',
                address: '重庆市渝中区满级们123',
                isDefault:false
            }
        ]

    }),
    getters: {
        allInfo(state) {
            let allDiscount=0
            let allNum = 0
            let allPrice = 0
            let allCheck = true
            let checkNum = 0
            let checkPrice = 0
            let isCheckArr = []
            // 尺寸
            let sizeAlias = null

            let {
                goodsList
            } = state
            goodsList.forEach(v => {
                // console.log(v);
                allDiscount += v.discountPrice
                allNum += v.productNum
                allPrice += v.productNum * v.discountPrice
                // console.log(v.isCheck);
                if (!v.isCheck) {
                    allCheck = false
                }
                // console.log(allCheck);
                if (v.isCheck) {
                    checkNum += v.productNum
                    checkPrice += checkNum * v.discountPrice
                    // console.log(checkPrice);
                    isCheckArr.push(v)
                    console.log(isCheckArr);
                }
            });
            return {
                allNum,
                allPrice,
                allCheck,
                checkNum,
                checkPrice,
                sizeAlias,
                isCheckArr,
            }
        }
    },
    mutations: {
    
        // 添加购物车
        cartAdd(state, obj) {
            for (let i in state.goodsList) {
                if (state.goodsList[i] == obj) {
                    console.log(state.goodsList[i]);
                    state.goodsList[i].productNum++
                    state.goodsList[i].sizeAlias = obj.sizeAlias
                    return;
                }
            }
            console.log(obj);
            // 参数
            // {Object | Array} target
            // {string | number} propertyName/index
            // {any} value
            Vue.set(obj, 'productNum', 1)
            Vue.set(obj, 'isCheck', true)
            state.goodsList.push(obj)
            // console.log(1);
            // console.log(state.goodsList);
        },
        // 计算数量 idx: 下标  n: 1 表示+  -1 表示-
        cartCal(state,{idx,n,stock}){
            // console.log(idx);
            // // console.log(n);
            // console.log(stock);
            let {goodsList} = state
            if (n == 1) {
                if (goodsList[idx].productNum >= stock) {
                    goodsList[idx].productNum = stock;
                } else {
                    goodsList[idx].productNum++;
                }
              } else {
                if (goodsList[idx].productNum <= 1) {
                    goodsList[idx].productNum = 1;
                } else {
                    goodsList[idx].productNum--;
                }
              }
        },
        // cartCheckOut(state){
        //     console.log(2);
        // }

        // 结算页删除商品
        cartDel(state,idx){
            state.goodsList.splice(idx, 1)
        },
        // 修改勾选状态 单选控制
        cartStatus(state,idx){
            // console.log(state.goodsList[idx].isCheck );
            state.goodsList[idx].isCheck = !state.goodsList[idx].isCheck
            // console.log(state.goodsList);
        },
        // 全选控制单选
        cartAllCheck(state,bool){
            console.log(bool);
            let {goodsList} = state
            goodsList.forEach((v)=>{
                v.isCheck=!bool
                console.log(v.isCheck);
            })
        },
    }
}